package com.qihetimes.earthangel.service.impl;

import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Redis服务
 *
 * @author ：f4cklangzi@gmail.com
 * @date ：Created At 2021/12/10
 */
@Service
public class RedisService {
    
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    
    
    /**
     * 写入缓存
     *
     * @param key    键 key
     * @param offset 偏移
     * @param value  位 8Bit=1Byte
     * @return boolean
     */
    public boolean setBit(String key, long offset, boolean value) {
        boolean result = false;
        try {
            ValueOperations<String, Object> operations = redisTemplate.opsForValue();
            operations.setBit(key, offset, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    
    /**
     * 写入缓存设置时效时间
     *
     * @param key        键
     * @param value      值
     * @param expireTime 有效期 秒
     * @return 结果
     */
    public boolean set(final String key, Object value, Long expireTime) {
        boolean result = false;
        try {
            ValueOperations<String, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    
    
    /**
     * 批量删除对应的value
     *
     * @param keys 键集合
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            remove(key);
        }
    }
    
    
    /**
     * 删除对应的value
     *
     * @param key 键
     */
    public void remove(final String key) {
        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }
    
    
    /**
     * 判断缓存中是否有对应的value
     *
     * @param key 键
     * @return 是否存在
     */
    public Boolean exists(final String key) {
        return redisTemplate.hasKey(key);
    }
    
    
    /**
     * 读取缓存
     *
     * @param key 键
     * @return 值
     */
    public Object get(final String key) {
        Object result;
        ValueOperations<String, Object> operations = redisTemplate.opsForValue();
        result = operations.get(key);
        return result;
    }
    
    
    /**
     * 哈希 添加
     *
     * @param key     键
     * @param hashKey hash键
     * @param value   值
     */
    public void hmSet(String key, Object hashKey, Object value) {
        HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
        hash.put(key, hashKey, value);
    }
    
    
    /**
     * 哈希获取数据
     *
     * @param key     键
     * @param hashKey hash键
     * @return 值
     */
    public Object hmGet(String key, Object hashKey) {
        HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
        return hash.get(key, hashKey);
    }
    
    
    /**
     * 列表添加
     *
     * @param key   键
     * @param value 值
     */
    public void lPush(String key, Object value) {
        ListOperations<String, Object> list = redisTemplate.opsForList();
        list.rightPush(key, value);
    }
    
    
    /**
     * 列表获取
     *
     * @param key   键
     * @param start 索引开始 0
     * @param end   索引结束
     * @return 集合
     */
    public List<Object> lRange(String key, long start, long end) {
        ListOperations<String, Object> list = redisTemplate.opsForList();
        return list.range(key, start, end);
    }
    
    
    /**
     * 集合添加
     *
     * @param key   键
     * @param value 值
     */
    public void add(String key, Object value) {
        SetOperations<String, Object> set = redisTemplate.opsForSet();
        set.add(key, value);
    }
    
    
    /**
     * 集合获取
     *
     * @param key 键
     * @return Set
     */
    public Set<Object> setMembers(String key) {
        SetOperations<String, Object> set = redisTemplate.opsForSet();
        return set.members(key);
    }
    
    
    /**
     * 有序集合添加
     *
     * @param key   键
     * @param value z值
     * @param score 排序分值
     */
    public void zAdd(String key, Object value, double score) {
        ZSetOperations<String, Object> zSet = redisTemplate.opsForZSet();
        zSet.add(key, value, score);
    }
    
    
    /**
     * 有序集合获取
     *
     * @param key      键
     * @param minScore 起始值
     * @param maxScore 结束值
     * @return 集合
     */
    public Set<Object> rangeByScore(String key, double minScore, double maxScore) {
        ZSetOperations<String, Object> zSet = redisTemplate.opsForZSet();
        redisTemplate.opsForValue();
        return zSet.rangeByScore(key, minScore, maxScore);
    }
    
    
    /**
     * 有序集合获取排名
     *
     * @param key   键 集合名称
     * @param value 值
     */
    public Long zRank(String key, Object value) {
        ZSetOperations<String, Object> zSet = redisTemplate.opsForZSet();
        return zSet.rank(key, value);
    }
    
    
    /**
     * 有序集合获取排名
     *
     * @param key 键
     */
    public Set<ZSetOperations.TypedTuple<Object>> zRankWithScore(String key, long start, long end) {
        ZSetOperations<String, Object> zSet = redisTemplate.opsForZSet();
        return zSet.rangeWithScores(key, start, end);
    }
    
    
    /**
     * 有序集合添加
     *
     * @param key   键
     * @param value 值
     */
    public Double zSetScore(String key, Object value) {
        ZSetOperations<String, Object> zSet = redisTemplate.opsForZSet();
        return zSet.score(key, value);
    }
    
    
    /**
     * 有序集合添加分数
     *
     * @param key   键
     * @param value 值
     * @param score 分数
     */
    public void incrementScore(String key, Object value, double score) {
        ZSetOperations<String, Object> zSet = redisTemplate.opsForZSet();
        zSet.incrementScore(key, value, score);
    }
    
    
    /**
     * 有序集合获取排名
     *
     * @param key 键
     */
    public Set<ZSetOperations.TypedTuple<Object>> reverseZRankWithScore(String key, long start, long end) {
        ZSetOperations<String, Object> zSet = redisTemplate.opsForZSet();
        return zSet.reverseRangeByScoreWithScores(key, start, end);
    }
    
    
    /**
     * 有序集合获取排名
     *
     * @param key 键
     */
    public Set<ZSetOperations.TypedTuple<Object>> reverseZRankWithRank(String key, long start, long end) {
        ZSetOperations<String, Object> zSet = redisTemplate.opsForZSet();
        return zSet.reverseRangeWithScores(key, start, end);
    }
    
    /**
     * 设置有效期
     *
     * @param key    键
     * @param expire 有效期 秒
     */
    public void expire(String key, long expire) {
        redisTemplate.expire(key, expire, TimeUnit.SECONDS);
    }
}
